package com.tom.commonframework.common.refreshview.base;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tom.commonframework.common.refreshview.listener.OnLoadMoreListener;

import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;

/**
 * Created by TomLeisen on 2018/7/27 下午1:55
 * Email: xy162162a@163.com
 * Description:上拉加载工具类
 */

public class LoadMoreHelper extends RecyclerView.OnScrollListener implements BaseLoadMoreAdapter.OnClickLoadMoreListener {

    private RecyclerView.LayoutManager mLayoutManager;
    private BaseLoadMoreAdapter mBaseLoadMoreAdapter;
    private OnLoadMoreListener mListener;
    //是否正在加载中
    private boolean mLoading = false;
    //上拉刷新功能是否开启
    private boolean mIsSwipeToLoadEnabled = true;

    public LoadMoreHelper(RecyclerView recyclerView, BaseLoadMoreAdapter baseLoadMoreAdapter) {
        mLayoutManager = recyclerView.getLayoutManager();
        mBaseLoadMoreAdapter = baseLoadMoreAdapter;
        mBaseLoadMoreAdapter.setClickLoadMoreListener(this);

        if (mLayoutManager instanceof GridLayoutManager) {
            mBaseLoadMoreAdapter.setAdapterType(BaseLoadMoreAdapter.ADAPTER_TYPE_GRID);
            mBaseLoadMoreAdapter.setSpanCount(((GridLayoutManager) mLayoutManager).getSpanCount());
        } else if (mLayoutManager instanceof LinearLayoutManager) {
            mBaseLoadMoreAdapter.setAdapterType(BaseLoadMoreAdapter.ADAPTER_TYPE_LINEAR);
        }
        recyclerView.addOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        if (mIsSwipeToLoadEnabled && SCROLL_STATE_IDLE == newState && !mLoading) {
            if (mLayoutManager instanceof GridLayoutManager) {
                final GridLayoutManager gridLayoutManager = (GridLayoutManager) mLayoutManager;
                gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        if (mIsSwipeToLoadEnabled) {
                            // 功能开启, 根据位置判断, 最后一个item时返回整个宽度, 其他位置返回1
                            if (position == mLayoutManager.getItemCount() - 1) {
                                return gridLayoutManager.getSpanCount();
                            } else {
                                return 1;
                            }
                        } else {
                            return 1;
                        }
                    }
                });
            }

            if (mLayoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mLayoutManager;
                int lastCompletePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if (lastCompletePosition == mLayoutManager.getItemCount() - 2) {
                    int firstCompletePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                    View child = linearLayoutManager.findViewByPosition(lastCompletePosition);
                    if (child == null)
                        return;
                    int deltaY = recyclerView.getBottom() - recyclerView.getPaddingBottom() - child.getBottom();
                    if (deltaY > 0 && firstCompletePosition != 0) {
                        recyclerView.smoothScrollBy(0, -deltaY);
                    }
                } else if (lastCompletePosition == mLayoutManager.getItemCount() - 1) {
                    mLoading = true;
                    mBaseLoadMoreAdapter.setLoadItemState(true);
                    if (mListener != null) {
                        mListener.onLoadMore();
                    }
                }
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
    }

    /**
     * 设置LoadMore Item为加载完成状态, 上拉加载更多完成时调用
     */
    public void loadComplete() {
        mLoading = false;
        mBaseLoadMoreAdapter.setLoadItemState(false);
    }


    /**
     * 上拉操作触发时调用的接口
     */
    public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        mListener = loadMoreListener;
    }

    @Override
    public void onClickLoad() {
        if (!mLoading && !mBaseLoadMoreAdapter.isLastView()) {
            mLoading = true;
            mBaseLoadMoreAdapter.setLoadItemState(true);
            mListener.onClickLoadMore();
        }
    }

}
